/*****************************************************************************************
**  Author:jxx 2022
**  QQ:283591387
**完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
**常用示例见：http://v2.volcore.xyz/document/vueDev
**后台操作见：http://v2.volcore.xyz/document/netCoreDev
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
// import modelHeader from "../../salesinfo/delivery_request/delivery_request.vue"
// import modelBody from "../../salesinfo/sales_contract/salesContract.vue"
import modelBody from "../../salesinfo/sales/sales.vue"
// import modelFooter from "../../salesinfo/sales/sales.vue"
// import modelFooter from "../../salesinfo/sales_contract_mx/salesContractMx.vue";
import modelFooter from "../contract_az_mx/contractAzMx.vue";
import { onMounted } from "vue";
import { mapMutations } from "vuex";
import gridHeader from "@/components/print/selectTemplet.vue"

let extension = {
  components: {
    //查询界面扩展组件
    gridHeader,
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    // modelHeader: modelHeader,
    modelBody: modelBody,
    modelFooter: modelFooter,
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮

  methods: {
    ...mapMutations(
      ("salesContractMX", ["storeRemoveForm", "storeChangeForm"])
    ),
    //下面这些方法可以保留也可以删除
    async onInit() {
      this.queryFields = [ "Client","Install_date"]
      this.boxOptions.labelWidth = 120;
      let startTime = this.base.getOneWeekAgoDate()
      let endTime = this.base.getDate(false) + " 23:59:59"
      this.searchFormFields.Install_date = [startTime, endTime]
      this.detailOptions.textInline = false
      this.textInline = false
      if (this.$route.path == "/q_install_request" && JSON.stringify(this.$route.query) != '{}') {
        console.log("触发查询")
        await this.base.setSearchParamFn(this)
        // this.searchFormFields.Voucher_sub = null
        // this.searchFormFields.Sales_sub = null
        // this.searchFormFields.Client = null
        // this.searchFormFields.Install_status = null
        // this.searchFormFields.AuditStatus = null
        // this.searchFormFields.Install_date = [null, null]
        // const selectList = ['Client', 'Install_status', 'AuditStatus', 'Install_date',]
        // this.searchFormOptions.forEach(item => {
        //   item.forEach(x => {
        //     if (selectList.includes(x.field)) {
        //       x.onChange = (v) => {
        //         this.$route.query = {}
        //       }
        //     }
        //   })
        // })
        setTimeout(() => {
          this.refresh()
        })
      }
    },
    async onInited() {
      this.single = true
      if (this.$route.path != "/q_install_request") {
        this.setFiexdSearchForm(true);
        this.height = 550 - 150;
        this.buttons.forEach(item => {
          if (item.value != "Search" ) {
            item.hidden = true
          }
        })
        this.searchFormFields.AuditStatus = "1"
        this.searchFormFields.Install_status = "2"
      }
      // await this.setFiexdSearchForm(true);
      // await this.buttons.forEach(item => {
      //   if (item.name == "高级查询") {
      //     item.hidden = true
      //   }
      // })


      await this.detailOptions.buttons.splice(this.detailOptions.buttons.findIndex(item => item.name == "添加行"), 1)
      await this.detailOptions.buttons.splice(this.detailOptions.buttons.findIndex(item => item.name == "删除行"), 1)
      await this.detailOptions.buttons.splice(this.detailOptions.buttons.findIndex(item => item.name == "导入"), 1)

      // this.detailOptions.buttons.unshift({
      //   name: "选择合同商品",//按钮名称
      //   icon: 'el-icon-plus',//按钮图标，参照iview图标
      //   type: 'primary',
      //   plain: true,
      //   onClick: function () { //触发事件
      //     if (!this.editFormFields.Sales_sub) {
      //       if (this.messageError) {
      //         this.messageError.close();
      //       }
      //       this.messageError = this.$message.error("请先选择销售合同");
      //       return
      //     }
      //     // if (this.rowschild) {
      //     //   this.rowschild.click()
      //     // }
      //     let form = {
      //       htid: this.editFormFields.Sales_sub
      //     }
      //     this.storeChangeForm(form);
      //     this.$refs.modelFooter.open(this.editFormFields.Sales, this.editFormFields.Sales_sub)
      //     this.$store.commit("setTabelText", false)
      //   }
      // })

    },
    async addBeforeAsync(formData) {
      formData.mainData.Install_status = "0"
      return this.testAdd(formData)
    },
    async updateBeforeAsync(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return this.testAdd(formData)
    },
    async searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      if (this.$route.path != "/q_install_request") {
        param.wheres = param.wheres.filter(item => item.name != "Install_request")
        this.$route.query = {}
      }
      if (this.$route.path == "/q_install_request" && JSON.stringify(this.$route.query) != '{}') {
        await this.base.setSearchBefore(this, "Install_request", param)
        // const sectionState = ["[null,null]", JSON.stringify(["", null]), JSON.stringify([null, ""]), JSON.stringify(["", ""])]
        // const dateState = sectionState.includes(JSON.stringify(this.searchFormFields.Install_date))
        // if (this.searchFormFields.Voucher_sub || this.searchFormFields.Sales_sub || !dateState) {
        //   param.wheres = param.wheres.filter(item => item.name != "Install_request")
        //   this.$route.query = {}
        // }
      }
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true;
    },
    delBefore(ids, rows) { //查询界面的表删除前 ids为删除的id数组,rows删除的行
      let auditStatus = rows.some(x => { return x.AuditStatus == 1 });
      if (auditStatus) {
        this.base.ElMessageFn('不支持删除审核通过数据')
        return false;
      }
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      this.$refs.table.$refs.table.toggleRowSelection(row);
    },
    detailRowClick({ row, column, event }) {
      if (this.$route.path != "/q_install_request" || this.rowAuditStatus != 0) { return };//|| !this.CreateUserBoole

      if (column.property == "Remark" ) {//|| column.property == "Goods_num"
        let dom = event.target
        if (dom.nodeName != "SPAN") {
          setTimeout(() => {
            let inpDom = dom.querySelector("input")
            inpDom.focus()
          }, 0)
        }
      }
    },
    onModelClose(iconClick) {
      //iconClick=true为点击左中上角X触发的关闭事件
      //如果返回 false不会关闭弹出框 
      this.storeRemoveForm()
      this.$store.commit("setTabelText", true)
      this.boxModel = false;
    },
    async modelOpenBefore(row) {
      this.$store.commit("setTabelText", false)
      this.CreateUserBoole = row.CreateID == this.$store.state.userID
      this.rowAuditStatus = await row.AuditStatus
      if (JSON.stringify(row) == "{}") {
        this.rowAuditStatus = 0
        this.CreateUserBoole = true
      }
      if (this.$route.path != "/q_install_request" || this.rowAuditStatus != 0) {//|| !this.CreateUserBoole
        this.boxButtons[0].hidden = true
        this.detailOptions.buttons.forEach(item => {
          if (item.name != "导出" && item.name != "刷新") {
            item.hidden = true
          }
        })
      } else {
        this.boxButtons[0].hidden = false
        this.detailOptions.buttons.forEach(item => item.hidden = false)
      }
    },
    modelOpenAfter(row) {
      this.detailOptions.beginEdit = (row, column, index) => {
        if (this.$route.path != "/q_install_request" || this.rowAuditStatus != 0) return false;//|| !this.CreateUserBoole
        return true
      };
      this.editFormOptions.forEach(item => {
        item.forEach(x => {
          if (x.field == "Sales" ) {
            try {
              x.hidden = true
            } catch (error) {
              console.error(error);
            }
          }
          if (x.field == "Voucher_sub"  || x.field == "Sales_sub" || x.field == "Client"|| x.field == "Install_status") {
            x.disabled = true
          } else {
            x.disabled = false
          }
          if (this.$route.path != "/q_install_request" || this.rowAuditStatus != 0) {//|| !this.CreateUserBoole
            x.disabled = true
            if (x.field == "Sales_sub") {
              x.extra = null
            }
          } else {
            if (x.field == "Sales_sub") {
              if(this.currentAction == "Add"){
                x.remote = true
                x.extra = {
                  icon: "el-icon-zoom-in",
                  text: "选择销售单",
                  style: "color:#3a8ee6;font-size:13px;cursor:default",
                  click: () => {
                    // if (!this.$refs.detail.rowData.length) {
                      this.$refs.modelBody.open()
                      this.$store.commit("setTabelText", false)
                    // } else {
                    //   this.base.ElMessageFn("存在安装申请单明细，不允许更改销售单")
                    // }
                  }
                }
              }else {
                x.extra = null
              }
            }
          }
        })
      })
    },
    async getSummary(formData) {
      let detailRowData = this.$refs.detail.rowData
      let summaryNum = 0
      await detailRowData.forEach(item => {
        summaryNum += +item.Goods_num
      })
      // formData.mainData.Goods_num = summaryNum
      return true
    },  
      async testAdd(formData) {
        formData.mainData.Install_status=4
      // if (!this.$refs.detail.rowData.length) {
      //   this.base.ElMessageFn('明细表不可为空', "error")
      //   return false
      // }
      // let boole = formData.detailData.some(item => item.Goods_num === "" || item.Goods_num === null || item.Goods_num === undefined)
      // if (boole) {
      //   this.base.ElMessageFn("明细表中请填写数量")
      //   return false;
      // }
      // let boole2 = formData.detailData.some(item => +item.Goods_num === 0 &&(item.Goods_price!=""||item.Goods_price!=null|| item.Goods_price == undefined))
      // if (boole2) {
      //   this.base.ElMessageFn("明细表中数量不可为零")
      //   return false;
      // }
      // let boole3 = formData.detailData.every(item => +item.Goods_num > 0 && +item.Goods_num)
      // if (!boole3) {
      //   this.base.ElMessageFn("明细表中数量请填写有效正数")
      //   return false;
      // }
      return true
    },
  },
};
export default extension;
